/*
 * @lc app=leetcode.cn id=404 lang=csharp
 *
 * [404] 左叶子之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public int SumOfLeftLeaves(TreeNode root) {
        if(root == null || (root.left == null && root.right == null)) return 0;
        
        int ans = 0;
        var queue = new Queue<TreeNode>();
        queue.Enqueue(root);
        
        while(queue.Count > 0)
        {
            var node = queue.Dequeue();
            
            if(node.left != null && (node.left.left == null && node.left.right == null))
                ans += node.left.val;
            else
            {
                if(node.left != null)
                    queue.Enqueue(node.left);
            }
            if(node.right != null)
                queue.Enqueue(node.right);
        }
        
        return ans;
    }
}
// @lc code=end

